package com.example.mobwe_project;

//Jussin testikommit

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Ebook implements Parcelable{
	
	// The first constructor without any parameters
	public Ebook(){
		
	}
	// The second constructor that takes a ebook json object
	public Ebook( JSONObject json ){
		
	}
	
	public Ebook( File file ){
		this.importFromFile(file);
	}
	
	private int mData;
	
	private String _id;
	public String getId() { return _id; }
	public void setId(String id) { _id = id; }
	
	private String _preview;
	public String getPreview() { return _preview; }
	public void setPreview(String p) { _preview = p; }
	
	private String _title;
	public String getTitle() { return _title; }
	public void setTitle(String t) { _title = t; }
	
	private String _author;
	public String getAuthor() { return _author; }
	public void setAuthor(String a) { _author = a; }
	
	private String _description;
	public String getDescription() { return _description; }
	public void setDescription(String d) { _description = d; }
	
	public enum Language { ENGLISH, SPANISH, RUMANIAN, FINNISH };
	private Language _language;
	public Language getLanguage() { return _language; }
	public void setLanguage(Language lg) { _language = lg; }
	
	
	public enum Theme { CAPITALISM, COMMUNICATION, COMPANIONSHIP, 
		DISILLUSIONMENT, EMPOWERMENT, RACISM, 
		REALITY, FAMILY, FEAR, GROWINGUP, FULFILLMENT, 
		BETRAYAL}; // ETCETERA
	private Theme _theme;
	public Theme getTheme() { return _theme; }
	public void setTheme(Theme tm) { _theme = tm; }
	
	
	private String[] _keywords;
	public String[] getKeywords() { return _keywords; }
	public void setKeywords(String[] k) { _keywords = k; }
	
	public boolean importFromFile( File file ){
		File infoJson = new File(file.getPath() + "/info.json");
		String jsonString = "";
		if( infoJson.exists() ){
			Log.d("EbookClass","info.json was opened");
			try {
				InputStream is = new FileInputStream( infoJson );
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				int i;
				Log.d("EbookClass","Starting to read the file");
				try {
					i = is.read();
					while( i != -1 ){
						byteArrayOutputStream.write( i );
						i = is.read();
					}
					Log.d("EbookClass","The reading of the file was ended");
					jsonString = byteArrayOutputStream.toString();
					Log.d("EbookClass","Following information was read: " + jsonString);
					is.close();						
				} catch (IOException e) {}								
			} catch (FileNotFoundException e) {}							
		}else{
			Log.d("EbookClass","File info.json doesn't exist");
			return false;
		}				

		if( this.parseFromJson( jsonString )){
			return true;
		}
		return false;
	}
	
	public static boolean fileIsEbook( File file ){
		File infoJson = new File(file.getPath() + "/info.json");
		if( infoJson.exists() ){
			return true;
		}else{
			return false;
		}
	}
	
	private Boolean parseFromJson( String json ){
		Log.d("EbookClass","Starting to parse the Ebook from a json file");
		try{
			JSONObject ebook = new JSONObject( json );
			JSONArray keywordsArray;
			String[] keywords;
			this.setId( ebook.getString("id") );
			this.setPreview( ebook.getString("preview") );
			this.setTitle( ebook.getString("title") );
			this.setAuthor( ebook.getString("author") );
			this.setDescription( ebook.getString("description") );
			//this.setLanguage( (Language) ebook.get("language") );
			//this.setTheme( (Theme)ebook.get("theme") );
			keywordsArray = ebook.getJSONArray("keywords");
			keywords = new String[ keywordsArray.length() ];
			for( int i = 0; i < keywordsArray.length(); i++){
				keywords[i] = keywordsArray.getJSONObject(i).getString("keyword");
			}
			this.setKeywords( keywords );
			Log.d("EbookClass","Parsing should be now finished and true will be returned");
		} catch( Exception e){
			Log.d("EbookClass","Something happened when parsing the file and false will be returned");
			Log.e("EbookClass", e.getMessage());
			return false;
		}
		return true;
	}
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void writeToParcel(Parcel arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	public static final Parcelable.Creator<Ebook> CREATOR = new Parcelable.Creator<Ebook>() {
		public Ebook createFromParcel( Parcel in ){
			return new Ebook(in);
		}

		@Override
		public Ebook[] newArray(int size) {
			return new Ebook[size];
		}
	};
	
	private Ebook( Parcel in ){
		this.mData = in.readInt();
	}
}
